#$Id$

package Feeds::GCF::Exception::ExcCCBroker;

use strict qw(vars);
use warnings ( FATAL => 'all' );

use Data::Dumper;
use Readonly;
use POSIX qw(strftime);

use Feeds::GCF::Constants qw(:GENERIC :STD_TABLE :STD_TYPE :SYBASE_SQL);
use Feeds::GCF::Util qw(hostname now today redo_sql);
use Feeds::Utils::CDSBrokerConfig;
use Feeds::Utils::CDSBrokerMapper;
use Feeds::GCF::Viper;
use Feeds::Utils::CDSBrokerDBHelper;
use FixInc::Halo::API::Audit;

### OBJECT CONSTRUCTOR ###
# Purpose: Configure the plugin with all general default settings
# Returns: A configured transform object
sub new {

    my ( $this, %param ) = @_;
    my $class = ref($this) || $this;

    # config defaults
    my %default = (
        'log'   => '',
        'dbh'   => '',
        'param' => {},
        'fmeta' => {},
    );

    # our actual object setup
    my %setup = ();

    # fields to interpolate
    my @fields = ( 'log', 'dbh', 'param', 'fmeta' );

    # interpolate to default or config field value. The incoming
    # parameter can have an optional preceding dash.
    foreach my $field (@fields) {
        if ( defined $param{$field} ) {
            $setup{$field} = $param{$field};
        }
        elsif ( defined $param{"-$field"} ) {
            $setup{$field} = $param{ "-" . $field };
        }
        else {
            $setup{$field} = $default{$field};
        }
    }

    # push through all of our config values into
    # our internal hash
    my $self = \%setup;
    bless $self, $class;

    $self->{xml_config} = Feeds::Utils::CDSBrokerConfig->new(
        log         => $self->{log},
        feed_type   => $self->{param}->{feed_type},
        config_file => $self->{fmeta}->{config_file},
        cfg_server  => $self->{param}->{param_server}
    );

    $self->{mapper} = Feeds::Utils::CDSBrokerMapper->new(
        feed_type => $self->{param}->{feed_type},
        log       => $self->{log},
        dbh       => $self->{dbh}
    );

    my $tmp_viper_hash
        = Feeds::GCF::Viper::getFullMatchResult( $self->{dbh}, $self->{log},
        $self->{xml_config}->{viper_type} );
    $self->{viper_hash} = $tmp_viper_hash->{I};    #for issuer
    
    $self->{tranche_tier_cd_def}={
                                     'A' => 'FRSTLS',
                                     'B' => 'JNRMEZ',
                                     'C' => 'SENIOR',
                                     'D' => 'SNRMEZ',
                                     'E' => 'SPSPSR',
                                     'F' => 'SUPSNR',
                                 };
                                 
                                 
    $self->{halo_dbh} = FixInc::Halo::API::Audit->connect(
                'server'   => $self->{param}->{server},
                'database' => 'dmo_creditds',
                'halodb'   => 'halo_creditds',
                'username' => 'dmo_ops',
        	    'password' => 'dmo_ops',
                'log'      => $self->{log},
				)
	     or $self->{log}->die("Attempting database connection failed");
	     
   $self->{dmo_dbhelper} = Feeds::Utils::CDSBrokerDBHelper->new(
        dbh        => $self->{halo_dbh},
        log        => $self->{log},
        xml_config => {},
        table_list => [ 'cds_tranche','cds_trch_contract' ],
        db_nm      => 'dmo_creditds',
    );

    # $self->{log}->info(__PACKAGE__, ": initializing $class object.");
    # util_init( log => $self->{log}, dbh => $self->{dbh} );
    $self->__init();

    return $self;
}

# this function can be override by sub-class
# set exceptions that this feed need to check
sub __init {
    my $self = shift;
    my $dbh  = $self->{dbh};
    my $log  = $self->{log};

    $self->{exc_properties} = {
        'tranche' => {

            WTM => {
                func     => 'check_WTM',
                descr    => 'can not mapping term',
                priority => 10,
            },

            IWI => {
                func     => 'check_idx_IWI',
                priority => 30,
                descr    => 'check all index identifer, red_cd, dmo_idx_id',

            },
            
            WTC => {
                
                func     => 'check_tranche_tier_cd',
                priority => 31,
                descr    => 'check and get tranche tier code',
            },
            
            TWI => {
                func     => 'check_tranche_TWI',
                priority => 40,
                descr    => 'check tranche identifer,include tranche_cd, dmo_tranche_id'

            },
            
            WPC => {
                func     => 'check_WPC',
                descr    => 'set price src cd and contributor cd',
                priority => 100,
            },

        },

    };

    return;

}

#sub class can override this function
# to set  $self->{exc_properties}
sub set_configuration {
    my $self = shift;
    my $dbh  = $self->{dbh};
    my $log  = $self->{log};
}

sub run {
    my $self = shift;
    my $dbh  = $self->{dbh};
    my $log  = $self->{log};

    $self->set_configuration();
    my $sn_candidates      = [];
    my $index_candidates   = [];
    my $tranche_candidates = [];
    my $volume_candidates = [];

#    if ( $self->{xml_config}->{signle_name_criterial} ) {
#        $sn_candidates = $self->handle_single_name();
#    }
#
#    if ( $self->{xml_config}->{index_criterial} ) {
#        $index_candidates = $self->handle_index();
#    }

    if ( $self->{xml_config}->{tranche_criterial} ) {
        $tranche_candidates = $self->handle_tranche();
    }
    
#    if ( $self->{xml_config}->{volume_criterial} ) {
#        $volume_candidates = $self->handle_volume();
#    }

    return ( $sn_candidates, $index_candidates, $tranche_candidates,$volume_candidates );

}

sub handle_volume{
   my $self = shift;
    my $dbh  = $self->{dbh};
    my $log  = $self->{log};
    
    $self->{log}->die("should implemented by subclass");
  
}

sub handle_single_name {
    my $self = shift;
    my $dbh  = $self->{dbh};
    my $log  = $self->{log};
    
    if (grep /sn/ , @{$self->{param}->{exclude}}){
        $log->info("exclude singlename");
        my $ret=[];
        return $ret;
    }

    my $candidates = $self->__get_candidates('sn');
    $log->info(
        scalar @$candidates
            . " single name need to be handle in ExceptionChecking" );
    my $count = 0;
    foreach my $asset (@$candidates) {
        $asset->{load_contract} = 'y';
        $asset->{load_quote}    = 'y';
        $asset->{type} ='SN';
        $asset->{status_cd}     = 'LOD';
        $asset->{exc_cd}        = [];
        $asset->{vendor_data}   = $self->get_vendor_data_for_sn($asset);
        $self->check_exception_for_sn($asset);

        if ( scalar @{ $asset->{exc_cd} } > 0 ) {
            $asset->{status_cd}     = 'EXC';
            $asset->{load_contract} = 'n';
            $asset->{load_quote}    = 'n';
            $asset->{vendor_data}->{n_tbl}->{ric_root}=undef;
        }
        else {
            $asset->{vendor_data}->{n_tbl}->{ric_root}
                = $asset->{mapping}->{contract}->{rec_cd}
                . $asset->{mapping}->{contract}->{term}
                . $asset->{mapping}->{contract}->{ric_ccy}
                . $asset->{mapping}->{contract}->{ric_seniority}
                . $asset->{mapping}->{contract}->{ric_doc_clause};

            my $sql = <<"SQL";
  		 update $self->{xml_config}->{n_tbl} 
  		 set ric_root='$asset->{vendor_data}->{n_tbl}->{ric_root}'
  		 where oid= $asset->{contract_id}
SQL
            $dbh->do($sql);
            $asset->{mapping}->{contract}->{ric_root}
                = $asset->{vendor_data}->{n_tbl}->{ric_root};
            #$self->check_sn_WRC($asset);
            $asset->{dmo_data} = $self->get_dmo_data_for_sn($asset);
            $self->check_priority_exception_for_sn($asset);
        }

        $count++;

        $log->info("$count single name processed") if ( $count % 100 == 0 );

    }

    $log->info(
        "end processing single name for  $self->{fmeta}->{info_src_cd}");
    return $candidates;

}

sub __get_candidates {
    my $self = shift;
    my $type = shift;          #type can be sn,idx, tranche
    my $dbh  = $self->{dbh};
    my $log  = $self->{log};

    $log->info("retrieve candidate for $type");
    my $type_condition;
    if ( $type eq 'tranche' ) {
        $type_condition = $self->{xml_config}->{tranche_criterial};
    }
    else {

        $log->die("unknow type:$type, can't retrieve candidates");

    }
    my $sql;

    if ( $self->{param}->{candidate_list}
        && scalar @{ $self->{param}->{candidate_list} } > 0 )
    {
        my $contract_list = join ",", @{ $self->{param}->{candidate_list} };
        $sql = "SELECT oid as contract_id , 'NVD' as status_cd
  	       FROM  $self->{xml_config}->{n_tbl}
  	       WHERE  oid in (" . $contract_list . ")
  	         AND $type_condition ";
    }
    else {

        my $top = '';
        if ( $self->{param}->{load_limit} ) {
            $top = " top $self->{param}->{load_limit} ";
        }
        my $recheck_mode=$self->__get_recheck_fl($type);
        $sql = <<"SQL";
     SELECT $top p.contract_id , status_cd
     FROM p_ent_stat p, $self->{xml_config}->{n_tbl} n
     WHERE p.status_cd in ('NVD','RLD')
       AND p.feed_type='$self->{fmeta}->{feed_type}'
       AND p.contract_id=n.$self->{xml_config}->{n_tbl_key}
       AND $type_condition
    UNION
     SELECT distinct $top  e.contract_id, 'EXC' as status_cd
       FROM p_ent_stat s,p_ent_exc e, m_exc_def m, $self->{xml_config}->{n_tbl} n
      WHERE m.recheck_fl>='$recheck_mode'
        AND m.active_fl='y'
        AND e.exc_cd=m.exc_cd
        AND e.feed_type='$self->{fmeta}->{feed_type}'
        AND s.status_cd='EXC'
        AND n.$self->{xml_config}->{n_tbl_key}=s.contract_id
        and n.$self->{xml_config}->{n_tbl_key}=e.contract_id
        and $type_condition   
SQL

    }

    $log->info("retrieve candiate sql:\n$sql");

    my $candidates = $dbh->selectall_hashes($sql)
        or $log->die( $dbh->errstr );
    return $candidates;
}

#there are three recheck level, A,B,C, the larger the charcter, 
# the more often  this exception be checked, if set recheck_fl=B, then both
#B and C exception will be checked, default is B
#B is applied to daily run, C applied to intra-day run'

sub __get_recheck_fl{

    my $self = shift;
    my $type = shift;  #type can be sn,idx, tranche
    return $self->{param}->{exc_recheck_fl} if ($self->{param}->{exc_recheck_fl}) ;
    
    #if this is the first run, generate a file, set exc_recheck_fl as 'B', daily run
    #if file already exists, set exc_recheck_fl as 'C', intraday run
    #the file will be removed by housekeeping
    my $file_nm="$self->{param}->{feed_dir}/$self->{param}->{feed_type}.$type.FIRSTRUN";
    if ( -e  $file_nm){
        return 'C';
    }else{
        open FILE, ">$file_nm" or $self->{log}->die("create file:$file_nm failed:$!");
        close FILE;
        return 'B';
    }
    #$self->{log}->die("i am here");
}

#in config file, load_contract has three possible value
# 1. y, means whatever condition, load contract, for LCDS and markit
# 2. EN, means if contract exists, not load contract, for normal SN broker, default value
# 3. n, means whatever condition, don't load contract
sub check_priority_exception_for_sn {
    my $self        = shift;
    my $asset       = shift;
    my $vendor_data = $asset->{vendor_data};
    my $dmo_data    = $asset->{dmo_data};
    my $dbh         = $self->{dbh};
    my $log         = $self->{log};
    
    if (@{ $asset->{exc_cd} } > 0){
    	return ;
    }

    if ( exists $dmo_data->{cds_sn_contract} ) {
        $asset->{status_cd} = 'LOD';
        if (   $self->{xml_config}->{load_contract}
            && $self->{xml_config}->{load_contract} eq 'y' )
        {
            $asset->{load_contract} = 'y';
        }
        else {
            $asset->{load_contract} = 'n';
        }
    }
    else {
        $asset->{status_cd} = 'LOD';
        if (   $self->{xml_config}->{load_contract}
            && $self->{xml_config}->{load_contract} eq 'n' )
        {
            $asset->{load_contract} = 'n';
        }
        else {
            $asset->{load_contract} = 'y';
        }
    }
}

sub get_vendor_data_for_sn {
    my $self  = shift;
    my $asset = shift;
    my $dbh   = $self->{dbh};
    my $log   = $self->{log};

    my $ret;

    #  get contract data

    my $sql = <<"SQL";
    SELECT * 
    FROM $self->{xml_config}->{n_tbl}
    WHERE oid = $asset->{contract_id}
SQL

    my $res = $dbh->selectall_hashes($sql) or $log->die( $dbh->errstr );

    if ( scalar @$res >= 1 ) {
        $ret->{n_tbl} = $res->[0];
    }

    return $ret;

}

sub get_dmo_data_for_sn {
    my $self  = shift;
    my $asset = shift;
    my $dbh   = $self->{dbh};
    my $log   = $self->{log};

    my $ret = {};

    if ( scalar @{ $asset->{exc_cd} } > 0 ) {
        $asset->{status_cd} = 'EXC';
        return $ret;
    }

    # get dmo contract data
    my $has_dmocontract = 'n';
    my $has_dmoquote = 'n';
    my $sql             = <<"SQL";
    SELECT * 
    FROM dmo_creditds..cds_sn_contract 
    WHERE party_id = $asset->{mapping}->{contract}->{party_id}
       AND tenor_cd = '$asset->{mapping}->{contract}->{tenor_cd}'
       AND currency_cd = '$asset->{mapping}->{contract}->{currency_cd}'
       AND seniority_cd = '$asset->{mapping}->{contract}->{seniority_cd}'
       AND doc_clause_cd = '$asset->{mapping}->{contract}->{doc_clause_cd}'
    --WHERE ric_root='$asset->{vendor_data}->{n_tbl}->{ric_root}'
SQL

    my $res = $dbh->selectall_hashes($sql) or $log->die( $dbh->errstr );
    if ( scalar @$res >= 1 ) {
        $ret->{cds_sn_contract} = $res->[0];
        $has_dmocontract = 'y';
    }

    # get dmo quote data
    if ( $has_dmocontract eq 'y' ) {
        #for penstab feed, idn_contrib_cd maybe undef
        if ($asset->{mapping}->{quote}->{idn_contrib_cd}){
            $sql = <<"SQL";
    	    SELECT * 
    	    FROM  dmo_creditds..cds_sn_quote
    	    WHERE sn_contract_id=$ret->{cds_sn_contract}->{contract_id}
    	      AND idn_contrib_cd = '$asset->{mapping}->{quote}->{idn_contrib_cd}'
	      
SQL
        }else{
            
            $sql = <<"SQL";
    	    SELECT * 
    	    FROM  dmo_creditds..cds_sn_quote
    	    WHERE sn_contract_id=$ret->{cds_sn_contract}->{contract_id}
    	      AND idn_contrib_cd is  NULL
	      
SQL
       
        }

        $res = $dbh->selectall_hashes($sql) or $log->die( $dbh->errstr );
        if ( scalar @$res >= 1 ) {
            $ret->{cds_sn_quote} = $res->[0];
            $has_dmoquote='y';
        }
        
        if ($has_dmoquote eq 'y'){
          #get id_obj_xref EPS
          $sql = <<"SQL";
           SELECT *
           FROM dmo_creditds..id_obj_xref
           WHERE obj_oid=$ret->{cds_sn_quote}->{quote_id}     
SQL
         $res = $dbh->selectall_hashes($sql) or $log->die( $dbh->errstr );
         foreach my $rec (@$res){
          if ($rec->{id_type_cd} eq 'EPS'){
             $ret->{id_obj_xref_eps} = $rec;
          }elsif ($rec->{id_type_cd} eq 'RIC' || $rec->{id_type_cd} eq 'ORC'){
             $ret->{id_obj_xref_ric} = $rec;
          }
         }
        }
    }

    return $ret;

}

sub handle_index {
    my $self = shift;
    my $dbh  = $self->{dbh};
    my $log  = $self->{log};
    
    if (grep /idx/ , @{$self->{param}->{exclude}}){
        $self->{log}->info("exclude index");
        my $ret=[];
        return $ret;
    }

    my $candidates = $self->__get_candidates('idx');
    $log->info(
        scalar @$candidates
            . " index need to be handle in ExceptionChecking" );
    my $count = 0;
    foreach my $asset (@$candidates) {
        $asset->{load_contract} = 'y';
        $asset->{load_quote}    = 'y';
        $asset->{type} ='IDX';
        $asset->{status_cd}     = 'LOD';
        $asset->{exc_cd}        = [];
        $asset->{vendor_data}   = $self->get_vendor_data_for_index($asset);
        $self->check_exception_for_index($asset);

        if ( scalar @{ $asset->{exc_cd} } > 0 ) {
            $asset->{status_cd}     = 'EXC';
            $asset->{load_contract} = 'n';
            $asset->{load_quote}    = 'n';
            $asset->{vendor_data}->{n_tbl}->{ric_root}=undef;
        }
        else {
            $asset->{vendor_data}->{n_tbl}->{ric_root}
                = $asset->{mapping}->{contract}->{idx_ticker}
                . $asset->{mapping}->{contract}->{term}
                . $asset->{mapping}->{contract}->{series};

            my $sql = <<"SQL";
  		 update $self->{xml_config}->{n_tbl} 
  		 set ric_root='$asset->{vendor_data}->{n_tbl}->{ric_root}'
  		 where oid= $asset->{contract_id}
SQL
            $dbh->do($sql);
            $asset->{mapping}->{contract}->{ric_root}
                = $asset->{vendor_data}->{n_tbl}->{ric_root};
            $asset->{dmo_data} = $self->get_dmo_data_for_index($asset);
            $self->check_priority_exception_for_index($asset);
        }

        $count++;
        $log->info("$count index were processed") if ( $count % 100 == 0 );
    }

    $log->info("end processing index for  $self->{fmeta}->{info_src_cd}");
    return $candidates;

}

sub get_vendor_data_for_index {
    my $self  = shift;
    my $asset = shift;
    my $dbh   = $self->{dbh};
    my $log   = $self->{log};

    my $ret;

    my $sql = <<"SQL";
    SELECT * 
    FROM $self->{xml_config}->{n_tbl}
    WHERE oid = $asset->{contract_id}
SQL

    my $res = $dbh->selectall_hashes($sql) or $log->die( $dbh->errstr );

    if ( scalar @$res >= 1 ) {
        $ret->{n_tbl} = $res->[0];
    }

    return $ret;

}

sub get_dmo_data_for_index {
    my $self  = shift;
    my $asset = shift;
    my $dbh   = $self->{dbh};
    my $log   = $self->{log};

    my $ret;

    #get dmo index contract
    my $has_contract = 'n';
    my $has_quote ='n';
    my $sql          = <<"SQL";
    SELECT * 
    FROM dmo_creditds..cds_idx_contract 
    WHERE ric_root='$asset->{vendor_data}->{n_tbl}->{ric_root}'
SQL

    my $res = $dbh->selectall_hashes($sql) or $log->die( $dbh->errstr );
    if ( scalar @$res >= 1 ) {
        $ret->{cds_idx_contract} = $res->[0];
        $has_contract = 'y';
    }

    if ( $has_contract eq 'y' ) {
        $sql = <<"SQL";
		 
		   SELECT *
		   FROM  dmo_creditds..cds_idx_quote
		   WHERE idx_contract_id=$ret->{cds_idx_contract}->{contract_id}
		     AND idn_contrib_cd = '$asset->{mapping}->{quote}->{idn_contrib_cd}'
SQL
        $res = $dbh->selectall_hashes($sql) or $log->die( $dbh->errstr );
        if ( scalar @$res >= 1 ) {
            $ret->{cds_idx_quote} = $res->[0];
            $has_quote='y';
        }
        
        if ($has_quote eq 'y'){
          #get id_obj_xref EPS/RIC
          $sql = <<"SQL";
           SELECT *
           FROM dmo_creditds..id_obj_xref
           WHERE obj_oid=$ret->{cds_idx_quote}->{quote_id}     
SQL
         $res = $dbh->selectall_hashes($sql) or $log->die( $dbh->errstr );
         foreach my $rec (@$res){
          if ($rec->{id_type_cd} eq 'EPS'){
             $ret->{id_obj_xref_eps} = $rec;
          }elsif ($rec->{id_type_cd} eq 'RIC' || $rec->{id_type_cd} eq 'ORC'){
             $ret->{id_obj_xref_ric} = $rec;
          }
         }
        }

    }

    return $ret;

}

sub handle_tranche {
    my $self = shift;
    my $dbh  = $self->{dbh};
    my $log  = $self->{log};

    if (grep /tranche/ , @{$self->{param}->{exclude}}){
        $self->{log}->info("exclude tranche");
        my $ret=[];
        return $ret;
    }
    
    my $candidates = $self->__get_candidates('tranche');
    $log->info(
        scalar @$candidates
            . " tranche need to be handle in ExceptionChecking" );
    my $count = 0;
    foreach my $asset (@$candidates) {
        $asset->{load_contract} = 'y';
        $asset->{load_quote}    = 'y';
        $asset->{type} ='TRCH';
        $asset->{status_cd}     = 'LOD';
        $asset->{exc_cd}        = [];
        $asset->{vendor_data}   = $self->get_vendor_data_for_tranche($asset);
        $self->check_exception_for_tranche($asset);

#        if ( scalar @{ $asset->{exc_cd} } > 0 ) {
#            $asset->{status_cd}     = 'EXC';
#            $asset->{load_contract} = 'n';
#            $asset->{load_quote}    = 'n';
#            $asset->{vendor_data}->{n_tbl}->{ric_root}=undef;
#        }
#        else {
#            $asset->{vendor_data}->{n_tbl}->{ric_root}
#                = $asset->{mapping}->{contract}->{idx_ticker}
#                . $asset->{mapping}->{contract}->{term}
#                . $asset->{mapping}->{contract}->{series}
#                . $asset->{mapping}->{contract}->{ric_tranche_cd};
#
#            my $sql = <<"SQL";
#  		 update $self->{xml_config}->{n_tbl} 
#  		 set ric_root='$asset->{vendor_data}->{n_tbl}->{ric_root}'
#  		 where oid= $asset->{contract_id}
#SQL
#            $dbh->do($sql);
#            $asset->{mapping}->{contract}->{ric_root}
#                = $asset->{vendor_data}->{n_tbl}->{ric_root};
#            $asset->{dmo_data} = $self->get_dmo_data_for_tranche($asset);
#            $self->check_priority_exception_for_tranche($asset);
#        }
#
#        $count++;
#        $log->info("$count index were processed") if ( $count % 100 == 0 );
    }

    $log->info("end processing index for  $self->{fmeta}->{info_src_cd}");
    return $candidates;

}

sub get_dmo_data_for_tranche {
    my $self  = shift;
    my $asset = shift;
    my $dbh   = $self->{dbh};
    my $log   = $self->{log};

    my ($ret,$sql);

    my $has_quote = 'n';
    if ($asset->{mapping}->{quote}->{idn_contrib_cd}){
     $sql = <<"SQL";
		 
		   SELECT *
		   FROM  dmo_creditds..cds_trch_quote
		   WHERE trch_contract_id=$asset->{mapping}->{contract}->{contract_id}
		     AND idn_contrib_cd = '$asset->{mapping}->{quote}->{idn_contrib_cd}'
SQL
    }else{
        $sql = <<"SQL";
		 
		   SELECT *
		   FROM  dmo_creditds..cds_trch_quote
		   WHERE trch_contract_id=$asset->{mapping}->{contract}->{contract_id}
		     AND idn_contrib_cd is NULL
SQL
    }
    my $res = $dbh->selectall_hashes($sql) or $log->die( $dbh->errstr );
    if ( scalar @$res >= 1 ) {
        $ret->{cds_trch_quote} = $res->[0];
        $has_quote='y';
    }
    
    if ($has_quote eq 'y'){
          #get id_obj_xref EPS/RIC
          $sql = <<"SQL";
           SELECT *
           FROM dmo_creditds..id_obj_xref
           WHERE obj_oid=$ret->{cds_trch_quote}->{quote_id}     
SQL
         $res = $dbh->selectall_hashes($sql) or $log->die( $dbh->errstr );
         foreach my $rec (@$res){
          if ($rec->{id_type_cd} eq 'EPS'){
             $ret->{id_obj_xref_eps} = $rec;
          }elsif ($rec->{id_type_cd} eq 'RIC' || $rec->{id_type_cd} eq 'ORC'){
             $ret->{id_obj_xref_ric} = $rec;
          }
         }
        }
    
    

    return $ret;

}

sub get_vendor_data_for_tranche {
    my $self  = shift;
    my $asset = shift;
    my $dbh   = $self->{dbh};
    my $log   = $self->{log};

    my $ret;

    my $sql = <<"SQL";
    SELECT * 
    FROM $self->{xml_config}->{n_tbl}
    WHERE oid = $asset->{contract_id}
SQL

    my $res = $dbh->selectall_hashes($sql) or $log->die( $dbh->errstr );

    if ( scalar @$res >= 1 ) {
        $ret->{n_tbl} = $res->[0];
    }

    return $ret;

}

sub check_priority_exception_for_tranche {
    my $self     = shift;
    my $asset    = shift;
    my $dmo_data = $asset->{dmo_data};
    my $dbh      = $self->{dbh};
    my $log      = $self->{log};

    $asset->{status_cd}     = 'LOD';
    $asset->{load_contract} = 'n';

}

#sub class can set exception list in init
#and each exception checking function will be called here
sub check_exception_for_tranche {
    my $self  = shift;
    my $asset = shift;
    my $data  = $asset->{vendor_data};

    my @tranche_exc = sort {
        $self->{exc_properties}->{tranche}->{$a}
            ->{priority} <=> $self->{exc_properties}->{tranche}->{$b}
            ->{priority}
        }
        keys %{ $self->{exc_properties}->{tranche} };

    foreach my $exc (@tranche_exc) {

        my $func = $self->{exc_properties}->{tranche}->{$exc}->{func};
        $self->$func( $asset, $data );
    }

}

sub check_priority_exception_for_index {
    my $self     = shift;
    my $asset    = shift;
    my $dmo_data = $asset->{dmo_data};
    my $dbh      = $self->{dbh};
    my $log      = $self->{log};

    $asset->{status_cd}     = 'LOD';
    $asset->{load_contract} = 'n';

}

#sub class can set exception list in init
#and each exception checking function will be called here
sub check_exception_for_sn {

    my $self  = shift;
    my $asset = shift;
    my $data  = $asset->{vendor_data};

    my @sn_exc = sort {
        $self->{exc_properties}->{sn}->{$a}
            ->{priority} <=> $self->{exc_properties}->{sn}->{$b}->{priority}
        }
        keys %{ $self->{exc_properties}->{sn} };

    foreach my $exc (@sn_exc) {

        my $func = $self->{exc_properties}->{sn}->{$exc}->{func};
        $self->$func( $asset, $data );
    }
}

#WCU wrong currency
sub check_WCU {
    my $self   = shift;
    my $asset  = shift;
    my $n_data = shift;

    my $ret
        = $self->{mapper}->map_ccy( $n_data->{n_tbl}, $self->{xml_config} );
    if (   $ret
        && @$ret == 2 )
    {
        $asset->{mapping}->{contract}->{ric_ccy}     = $ret->[0];
        $asset->{mapping}->{contract}->{currency_cd} = $ret->[1];
    }
    else {
        push @{ $asset->{exc_cd} }, 'WCU';
    }

}

#set price src cd and contributor cd
sub check_WPC {
    my $self  = shift;
    my $asset = shift;
    my $data  = shift;

    $asset->{mapping}->{quote}->{price_src_cd}
        = $self->{fmeta}->{price_src_cd};
    $asset->{mapping}->{quote}->{idn_contrib_cd}
        = $self->{fmeta}->{contributor_cd};

}

# check non-priced-ric
# priced_fl will be set during nomalization phase
# here, just need check priced_fl and update p_non_priced_ric 
sub check_NPR {
    my $self  = shift;
    my $asset = shift;
    my $n_data  = shift;
    
   if (@{ $asset->{exc_cd} } > 0){
   	  return;
   }
   
   if ($asset->{type} eq 'SN'){
   
	   $asset->{vendor_data}->{n_tbl}->{ric_root}
	                = $asset->{mapping}->{contract}->{rec_cd}
	                . $asset->{mapping}->{contract}->{term}
	                . $asset->{mapping}->{contract}->{ric_ccy}
	                . $asset->{mapping}->{contract}->{ric_seniority}
	                . $asset->{mapping}->{contract}->{ric_doc_clause};
   }elsif($asset->{type} eq 'IDX'){
   		 $asset->{vendor_data}->{n_tbl}->{ric_root}
                = $asset->{mapping}->{contract}->{idx_ticker}
                . $asset->{mapping}->{contract}->{term}
                . $asset->{mapping}->{contract}->{series};
   }elsif($asset->{type} eq 'TRCH'){
   		 $asset->{vendor_data}->{n_tbl}->{ric_root}
                = $asset->{mapping}->{contract}->{idx_ticker}
                . $asset->{mapping}->{contract}->{term}
                . $asset->{mapping}->{contract}->{series}
                . $asset->{mapping}->{contract}->{ric_tranche_cd};
   }
   
   my $sql;
   
   if ($n_data->{n_tbl}->{priced_fl}
        &&  $n_data->{n_tbl}->{priced_fl} eq 'y' ){
   	  #try to remove record from p_non_priced_ric
   	  $sql=<<"SQL";
          delete from p_non_priced_ric
           where idn_contrib_cd = '$self->{fmeta}->{contributor_cd}'
             and ric_root = '$asset->{vendor_data}->{n_tbl}->{ric_root}'	  
SQL
      $self->{dbh}->do($sql);
   }else{
      #try to insert a  record in p_non_priced_ric 
      $sql=<<"SQL";
          delete from p_non_priced_ric
           where idn_contrib_cd = '$self->{fmeta}->{contributor_cd}'
             and ric_root = '$asset->{vendor_data}->{n_tbl}->{ric_root}'	
             
          insert into p_non_priced_ric(ric_root,idn_contrib_cd,feed_type,setup_dt)
          values('$asset->{vendor_data}->{n_tbl}->{ric_root}','$self->{fmeta}->{contributor_cd}'
                 ,'$self->{param}->{feed_type}',getdate())
          
SQL
      $self->{dbh}->do($sql);
      
      push @{ $asset->{exc_cd} }, 'NPR';
      $self->{log}->warning("this is a non-priced-ric:$asset->{vendor_data}->{n_tbl}->{ric_root}");
   }
   
   #$self->{log}->die("wait implement check_NPR");
   

}

#check tenor
sub check_WTM {
    my $self   = shift;
    my $asset  = shift;
    my $n_data = shift;

    if ( $self->{xml_config}->{multi_term} ) {
        $asset->{mapping}->{contract}->{term}
            = $n_data->{n_tbl}->{ $self->{xml_config}->{term_src_field} };
        $asset->{mapping}->{contract}->{tenor_cd}
            = $n_data->{n_tbl}->{ $self->{xml_config}->{term_src_field} };
        push @{ $asset->{exc_cd} }, 'WTM' unless $asset->{mapping}->{contract}->{term};
    }
    else {
        $asset->{mapping}->{contract}->{term} = $self->{mapper}
            ->map_term( $n_data->{n_tbl}, $self->{xml_config} );
        $asset->{mapping}->{contract}->{tenor_cd}
            = $asset->{mapping}->{contract}->{term};
        if ( not $asset->{mapping}->{contract}->{term} ) {
            push @{ $asset->{exc_cd} }, 'WTM';
        }
    }
}

#check tier,seniority
sub check_WSN {
    my $self   = shift;
    my $asset  = shift;
    my $n_data = shift;

    my $ret = $self->{mapper}
        ->map_seniority( $n_data->{n_tbl}, $self->{xml_config} );
    if (   $ret
        && @$ret == 2 )
    {
        $asset->{mapping}->{contract}->{ric_seniority} = $ret->[0];
        $asset->{mapping}->{contract}->{seniority_cd}  = $ret->[1];
    }
    else {
        push @{ $asset->{exc_cd} }, 'WSN';
    }

}

#check doc clause
sub check_WDC {
    my $self   = shift;
    my $asset  = shift;
    my $n_data = shift;

    my $ret = $self->{mapper}
        ->map_docclause( $n_data->{n_tbl}, $self->{xml_config} );
    if (   $ret
        && @$ret == 2 )
    {
        $asset->{mapping}->{contract}->{ric_doc_clause} = $ret->[0];
        $asset->{mapping}->{contract}->{doc_clause_cd}  = $ret->[1];
    }
    else {
        push @{ $asset->{exc_cd} }, 'WDC';
    }

}

#NIS means No Issuer
sub check_sn_NIS {
    my $self  = shift;
    my $asset = shift;
    my $data  = shift;

    my $cds;

    if ( $self->{xml_config}->{viper_type} ne 'RED' ) {
        $cds = {
            $data->{n_tbl}->{oid} => {
                issuer_id => $data->{n_tbl}
                    ->{ $self->{xml_config}->{viper_issuer_id} },
                issuer_name => $data->{n_tbl}
                    ->{ $self->{xml_config}->{viper_issuer_name} },
            },
        };
    }
    else {
        $cds = {
            $data->{n_tbl}->{oid} => {
                issuer_id => $data->{n_tbl}
                    ->{ $self->{xml_config}->{viper_issuer_id} },
            },
        };
    }

#my $result = Feeds::GCF::Viper::getMatchResultByHash($self->{dbh},
#                                                     $self->{log},
#                                                     "$self->{xml_config}->{viper_type}",
#                                                     'cds_broker_feed',
#                                                     $cds);

#if ($result->{$data->{n_tbl}->{oid}}->{match_id}
#     && $result->{$data->{n_tbl}->{oid}}->{rec_cd}){
#	$asset->{mapping}->{contract}->{rec_cd}= $result->{$data->{n_tbl}->{oid}}->{rec_cd};
#	$asset->{mapping}->{contract}->{party_id}= $result->{$data->{n_tbl}->{oid}}->{match_id};
#}else{
#	push @{$asset->{exc_cd}},'NIS';
#}

    if ( $data->{n_tbl}->{ $self->{xml_config}->{viper_issuer_id} }
        && exists $self->{viper_hash}
        ->{ $data->{n_tbl}->{ $self->{xml_config}->{viper_issuer_id} } } )
    {
        my $viper_record = $self->{viper_hash}
            ->{ $data->{n_tbl}->{ $self->{xml_config}->{viper_issuer_id} } };
        if (   $viper_record->{match_id}
            && $viper_record->{rec_cd} )
        {
            $asset->{mapping}->{contract}->{rec_cd} = $viper_record->{rec_cd};
            $asset->{mapping}->{contract}->{party_id}
                = $viper_record->{match_id};
        }
        else {
            push @{ $asset->{exc_cd} }, 'NIS';

            #call viper interfact to set viper related field
            Feeds::GCF::Viper::getMatchResultByHash( $self->{dbh},
                $self->{log}, "$self->{xml_config}->{viper_type}",
                'cds_broker_feed', $cds );
        }

    }
    else {
        push @{ $asset->{exc_cd} }, 'NIS';

        #call viper interfact to set viper related field
        Feeds::GCF::Viper::getMatchResultByHash( $self->{dbh}, $self->{log},
            "$self->{xml_config}->{viper_type}",
            'cds_broker_feed', $cds );
    }

}

#REC code may not consistent between feeds_common..m_party_rec_xref and dmo_creditds..cds_sn_contract
sub check_sn_WRC{
	my $self  = shift;
  my $asset = shift;
  my $data  = $asset->{vendor_data};
  my $dbh = $self->{dbh};
  
  if($asset->{mapping}->{contract}->{ric_root} ){
  	 
  	 my $sql=<<"SQL";
  	 
  	 select  top 1 ric_root 
  	 from dmo_creditds..cds_sn_contract
  	 where party_id=$asset->{mapping}->{contract}->{party_id}
  	   and tenor_cd= '$asset->{mapping}->{contract}->{tenor_cd}'
       and currency_cd= '$asset->{mapping}->{contract}->{currency_cd}'
       and seniority_cd='$asset->{mapping}->{contract}->{seniority_cd}'
       and doc_clause_cd='$asset->{mapping}->{contract}->{doc_clause_cd}'
SQL
    my $res=$dbh->selectall_hashes($sql);
    if (@$res > 0
        && $res->[0]->{ric_root} ne $asset->{mapping}->{contract}->{ric_root}){
         $self->{log}->warning("REC not consistent for partyid:$asset->{mapping}->{contract}->{party_id}");
         push @{ $asset->{exc_cd} }, 'WRC';
         $asset->{status_cd}     = 'EXC';
         $asset->{load_contract} = 'n';
         $asset->{load_quote}    = 'n';
    }
  }

}

#sub class can set exception list in init
#and each exception checking function will be called here
sub check_exception_for_index {
    my $self  = shift;
    my $asset = shift;
    my $data  = $asset->{vendor_data};

    my @idx_exc = sort {
        $self->{exc_properties}->{idx}->{$a}
            ->{priority} <=> $self->{exc_properties}->{idx}->{$b}->{priority}
        }
        keys %{ $self->{exc_properties}->{idx} };

    foreach my $exc (@idx_exc) {

        my $func = $self->{exc_properties}->{idx}->{$exc}->{func};
        $self->$func( $asset, $data );
    }

}

#index wrong identifier
sub check_idx_IWI {
    my $self       = shift;
    my $asset      = shift;
    my $data       = shift;
    my $mapper     = $self->{mapper};
    my $xml_config = $self->{xml_config};

    my $idx_id_nm = $mapper->map(
        {   field_nm   => idx_id_nm,
            norm_obj   => $self,
            record     => $data->{n_tbl},
            xml_config => $xml_config
        }
    );
    if ( not $idx_id_nm ) {
        push @{ $asset->{exc_cd} }, 'IWI';
        return;
    }

    $asset->{mapping}->{contract}->{idx_id_nm} = $idx_id_nm;
    my $name = $data->{n_tbl}->{ $self->{xml_config}->{viper_issuer_name} }
        || $data->{n_tbl}->{ $self->{xml_config}->{entitynm_src_field} }
        || undef;

    my ( $series, $version, $idx_ticker, $idx_id )
        = $mapper->get_idx_info_by_viper( $idx_id_nm, $name,
        $self->{xml_config}->{viper_type} );
    if ( $series && $version && $idx_ticker && $idx_id ) {
        $asset->{mapping}->{contract}->{idx_ticker} = $idx_ticker;
        $asset->{mapping}->{contract}->{series}     = $series;
        $asset->{mapping}->{contract}->{version}    = $version;
        $asset->{mapping}->{contract}->{idx_id}     = $idx_id;
    }
    else {
        push @{ $asset->{exc_cd} }, 'IWI';
    }

}

sub check_idx_ICE {
    my $self       = shift;
    my $asset      = shift;
    my $data       = shift;
    my $mapper     = $self->{mapper};
    my $xml_config = $self->{xml_config};

    unless ( $asset->{mapping}->{contract}->{idx_ticker}
        && $asset->{mapping}->{contract}->{term} )
    {

        #push @{$asset->{exc_cd}},'ICE';
        return;
    }

    my $ric_root = $asset->{mapping}->{contract}->{idx_ticker}
        . $asset->{mapping}->{contract}->{term}
        . $asset->{mapping}->{contract}->{series};

    my $sql
        = "SELECT 1 FROM dmo_creditds..cds_idx_contract WHERE ric_root='$ric_root' ";
    my $res = $self->{dbh}->selectall_hashes($sql);

    if ( scalar @$res < 1 ) {
        push @{ $asset->{exc_cd} }, 'ICE';
    }

}

sub gfi_get_red {
    my $self   = shift;
    my $params = shift;
    my $data   = $params->{record};
    my $dbh    = $self->{dbh};

    my $sql
        = "select top 1 index_red_cd from index_terms where gfi_idx_ent_symbol='$data->{gfi_cd}'";
    my $res = $dbh->selectall_hashes($sql);
    if ( scalar @$res == 1 ) {
        return $res->[0]->{index_red_cd};
    }
    else {
        return undef;
    }
}

sub check_tranche_tier_cd{
    my $self = shift;
    $self->{log}->die("please implement check_tranche_tier_cd in subclass");
    
}

#'check tranche identifer
# maybe need to create cds_tranche, cds_trch_contract
sub check_tranche_TWI {
    my $self   = shift;
    my $asset  = shift;
    my $n_data = shift;
    my $idx_id = $asset->{mapping}->{contract}->{idx_id};
    my $idx_trch_tier_cd = $asset->{mapping}->{contract}->{idx_trch_tier_cd};

    unless ( $asset->{mapping}->{contract}->{idx_ticker}
        && $asset->{mapping}->{contract}->{term} 
        && $asset->{mapping}->{contract}->{ric_tranche_cd})
    {

#        $self->{log}->warning("can't mapping trch_id without idx_id");
        return;
    }
    
    if ($asset->{mapping}->{contract}->{idx_id} and $asset->{mapping}->{contract}->{idx_trch_tier_cd}) {
    } else {
        return;
    }
    #check consistitent tier_cd and leg_nm 
    my $leg_num = ord($asset->{mapping}->{contract}->{ric_tranche_cd}) - 64;
    if ($leg_num != $asset->{mapping}->{contract}->{idx_trch_leg_nm}){
        $self->{log}->die("inconsistent with idx_trch_leg_nm and ric_tranche_cd\n"
                          .Dumper($asset->{mapping}->{contract})
                          ."\n".Dumper($asset->{vendor_data}->{n_tbl}));
    }
    
    if (  $asset->{mapping}->{contract}->{idx_trch_tier_cd} ne 
         $self->{tranche_tier_cd_def}->{$asset->{mapping}->{contract}->{ric_tranche_cd}}){
        
          $self->{log}->die("inconsistent with ric_tranche_cd and idx_trch_tier_cd\n"
                          .Dumper($asset->{mapping}->{contract})
                          ."\n".Dumper($asset->{vendor_data}->{n_tbl}));
    }

    my $idx2 = undef;
    my $flag1 = undef;
    my $flag2 = undef;
    my $flag3 = undef;
    my $flag4 = undef;
    my $flag5 = undef;
    my $flag6 = undef;
    my $sql = <<"SQL";
  	  select active_fl from dmo_creditds..cds_index
  	   where idx_id = $idx_id
         and active_fl = 'y'
SQL
    my $res = $self->{dbh}->selectall_hashes($sql);
    if ( scalar @$res != 1 ) {
        $flag1 = 0;
        $sql = <<"SQL";
          select t1.idx_id as idx2 from dmo_creditds..cds_index t1 inner join dmo_creditds..cds_index t2 
              on t2.idx_id = $idx_id
             and t1.active_fl = 'y'
             and t1.family_cd = t2.family_cd
             and t1.sub_family_cd = t2.sub_family_cd
             and t1.series_nm = t2.series_nm
SQL
        $res = $self->{dbh}->selectall_hashes($sql);

        if ( scalar @$res > 0 ) {
            $flag4 = 1;
            $idx2 = $res->[0]->{idx2};
        }
        
        if (not defined $idx2) {
            $flag4 = 0;
            $sql = <<"SQL";
              select t1.idx_id as idx2 from dmo_creditds..cds_index t1 inner join dmo_creditds..cds_index t2 
                  on t2.idx_id = $idx_id
                 and t1.family_cd = t2.family_cd
                 and t1.sub_family_cd = t2.sub_family_cd
                 and t1.series_nm = t2.series_nm
                 and t1.version_nm = (
                    select max(version_nm) from dmo_creditds..cds_index t3
                        where t2.idx_id = $idx_id
                        and t3.family_cd = t2.family_cd
                        and t3.sub_family_cd = t2.sub_family_cd
                        and t3.series_nm = t2.series_nm
                 )
SQL
            $res = $self->{dbh}->selectall_hashes($sql);
            if ( scalar @$res == 1 ) {
                $flag6 = 1;
                $idx2 = $res->[0]->{idx2};
            }
        }
        $asset->{mapping}->{contract}->{idx_id} = $idx2;
    } else {
        $flag1 = 1;
    }
        
    # Check tranche existed by current index and idx_trch_tier_cd
    $sql =<<"SQL";
    select trch_id 
     from dmo_creditds..cds_tranche 
     where idx_id= $asset->{mapping}->{contract}->{idx_id} 
     and idx_trch_tier_cd='$asset->{mapping}->{contract}->{idx_trch_tier_cd}'
    
SQL
    $res = $self->{dbh}->selectall_hashes($sql);
    if ( @$res == 1 ) {
        $flag2 = 1;
        $asset->{mapping}->{contract}->{trch_id}
            = $res->[0]->{trch_id};
    } else {
        $flag2 = 0;
        if (not exists $asset->{mapping}->{contract}->{attach}
         || not defined $asset->{mapping}->{contract}->{attach}){
            # Not enough info to create Tranche
            $flag5 = 0;
            push @{ $asset->{exc_cd} }, 'TWI'; 
            return;
        } else {
            $flag5 = 1;
#            $self->{log}->info("try to insert cds_tranch(cds_tranche) ");
#            $asset->{mapping}->{contract}->{trch_id}=$self->ins_cds_trch($asset,$n_data);
        }
    }

    $asset->{vendor_data}->{n_tbl}->{ric_root}
        = $asset->{mapping}->{contract}->{idx_ticker}
        . $asset->{mapping}->{contract}->{term}
        . $asset->{mapping}->{contract}->{series}
        . $asset->{mapping}->{contract}->{ric_tranche_cd};

    #get tranche contract id
    $sql = <<"SQL";
  	 
  	  SELECT TOP 1 contract_id 
  	    FROM dmo_creditds..cds_trch_contract
  	   WHERE ric_root=  '$asset->{vendor_data}->{n_tbl}->{ric_root}'
  	     
SQL

    $res = $self->{dbh}->selectall_hashes($sql);
    if ( @$res == 1 ) {
        $flag3 = 1;

        $asset->{mapping}->{contract}->{contract_id}
            = $res->[0]->{contract_id};
    }
    else {
        $flag3 = 0;
#        #use current trch_id to create contract
#        $self->{log}->info("try to insert cds_trch_contract(cds_tranche) ");
#        $asset->{mapping}->{contract}->{contract_id}=$self->ins_cds_trch_contract($asset,$n_data);
    }
    
    #当前index id存在且为活性的场合
    #  当前tranche id存在的场合
    #    当前contract存在的场合
    if ($flag1 == 1 and $flag2 == 1 and $flag3 == 1) {
        $self->{log}->info("case1 contract_id is: ".$asset->{contract_id});
    }
    if ($flag1 == 0 and $flag4 == 1 and $flag2 == 1 and $flag3 == 0) {
        $self->{log}->info("case2 contract_id is: ".$asset->{contract_id});
    }
    if ($flag1 == 0 and $flag4 == 1 and $flag2 == 1 and $flag3 == 1) {
        $self->{log}->info("case3 contract_id is: ".$asset->{contract_id});
    }
    if ($flag1 == 0 and $flag4 == 1 and $flag2 == 0 and $flag5 == 0) {
        $self->{log}->info("case4 contract_id is: ".$asset->{contract_id});
    }
    if ($flag1 == 0 and $flag4 == 1 and $flag2 == 0 and $flag5 == 1 and $flag3 == 1) {
        $self->{log}->info("case5 contract_id is: ".$asset->{contract_id});
    }
    if ($flag1 == 0 and $flag4 == 0 and $flag6 == 1 and $flag2 == 1 and $flag3 == 1) {
        $self->{log}->info("case6 contract_id is: ".$asset->{contract_id});
    }

}

#sub ins_cds_trch{
#    my $self   = shift;
#    my $asset  = shift;
#    my $n_data = shift;
#    my $halo_dbh = $self->{halo_dbh};
#
#    my $changesetid = $halo_dbh->changeset_create( "insert cds_tranche" );
#    my $rec={
#               op_type=>'I',
#               trch_id => $self->{dbh}->new_oid(),
#               idx_id  => $asset->{mapping}->{contract}->{idx_id} ,
#               idx_trch_leg_nm => $asset->{mapping}->{contract}->{idx_trch_leg_nm},
#               standard_attachment => $asset->{mapping}->{contract}->{attach},
#               standard_detachment => $asset->{mapping}->{contract}->{detach},
#               idx_trch_tier_cd  => $asset->{mapping}->{contract}->{idx_trch_tier_cd},
#               
#            };
#            
#    my $sql=$self->{dmo_dbhelper}->gen_sql('cds_tranche',$rec,$self->{log});
#    
#    $self->{log}->info("try to insert cds_tranche by sql\n".$sql);
#    $halo_dbh->do($sql);
#    
#    $halo_dbh->changeset_close( $changesetid );
#    
#    return $rec->{trch_id};
#}

#sub ins_cds_trch_contract{
#    my $self   = shift;
#    my $asset  = shift;
#    my $n_data = shift;
#    my $halo_dbh = $self->{halo_dbh};
#
#    my $changesetid = $halo_dbh->changeset_create( "insert cds_trch_contract" );
#    
#    my $trch_contract={
#                         op_type => 'I',
#                         contract_id => $self->{dbh}->new_oid(),
#                         trch_id => $asset->{mapping}->{contract}->{trch_id},
#                         tenor_cd => $asset->{mapping}->{contract}->{term},
#                         ric_root => $asset->{vendor_data}->{n_tbl}->{ric_root},
#                         contract_setup_dt => ':getdate():',
#                         contract_last_chg_dt => ':getdate():',
#                         active_fl => 'y',
#                      };
#    my $sql=$self->{dmo_dbhelper}->gen_sql('cds_trch_contract',$trch_contract,$self->{log});
#    $self->{log}->info("try to insert cds_trch_contract by sql\n".$sql);
#    $halo_dbh->do($sql);
#    
#    $halo_dbh->changeset_close( $changesetid );
#    
#    return $trch_contract->{contract_id};
#}
 
1;
